package com.six.elemo_manger.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import com.six.elemo_manger.R;

import java.io.FileNotFoundException;

import static android.app.Activity.RESULT_OK;
import static android.content.ContentValues.TAG;

public class Fra_add extends Fragment {
    View view;
    private static int RESULT_LOAD_IMAGE = 1;
    ProgressDialog dialog = null;
    private AlertDialog al;
    private String path;//图片地址
    private ImageView select_img;//图片
    private String imgUrl="";//图片URL
    private EditText name,price,detail;//图片
    private String  food_Types="美食";//FOOD type
    private TextView add;
    private RadioButton r1,r2,r3,r4,r5,r6,r7;
    /*
    添加
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.layout_add,container,false);

         return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
          init();
          select_img.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  choose_img();
              }
          });
          add.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  addFood();
              }
          });
          init();
    }
     void init(){
         select_img=view.findViewById(R.id.choose_img);
         name=view.findViewById(R.id.food_name);
         price=view.findViewById(R.id.food_price);
         detail=view.findViewById(R.id.et_content);
         add=view.findViewById(R.id.tv_send);
         r1=view.findViewById(R.id.radio_1);
         r2=view.findViewById(R.id.radio_2);
         r3=view.findViewById(R.id.radio_3);
         r4=view.findViewById(R.id.radio_4);
         r5=view.findViewById(R.id.radio_5);
         r6=view.findViewById(R.id.radio_6);
         r7=view.findViewById(R.id.radio_7);
     }
    /*
    选择图片
     */
    public void choose_img() {
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            path = cursor.getString(columnIndex);
            cursor.close();
            select_img.setImageURI(selectedImage);

        }

    }

    private void addFood(){
        showDialog();
        if (r1.isChecked()){
            food_Types="水果";
        }else if(r2.isChecked()){
            food_Types="甜品饮品";
        }else if(r3.isChecked()){
            food_Types="速食简吃";
        }else if(r4.isChecked()){
            food_Types="休闲零食";
        }else if(r5.isChecked()){
            food_Types="烧烤海鲜";
        }else if(r6.isChecked()){
            food_Types="包子米粥";
        }else if(r7.isChecked()){
            food_Types="无辣不欢";
        }
        final String foodname=name.getText().toString();
        final String foodprice=price.getText().toString();
        final String details=detail.getText().toString();

        if (path!=null|| TextUtils.isEmpty(foodname)|| TextUtils.isEmpty(foodprice)|| TextUtils.isEmpty(details)){
            //上传图片
            try {
                final AVFile file = AVFile.withAbsoluteLocalPath(path, path);
                file.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {

                            Log.d(TAG, file.getUrl());//返回一个唯一的 Url 地址
                            AVObject todoFolder = new AVObject("FoodList");// 构建对象
                            todoFolder.put("food_Name", foodname);// 设置名称
                            todoFolder.put("food_Price", foodprice+"元");// 设置价格
                            todoFolder.put("food_Detail", details);// 设置详情
                            todoFolder.put("food_Img", file.getUrl());// 设置图片
                            todoFolder.put("food_isPay", 0);// 设置是否购买
                            todoFolder.put("food_Type", food_Types);// 设置是否购买
                            todoFolder.put("priority", 1);// 设置优先级
                            todoFolder.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e==null){
                                    toast("上架成功");
                                    al.dismiss();
                                }else {
                                    toast("上架失败，请检查");
                                    Log.d("lmm",e.toString());
                                    al.dismiss();
                                }
                            }
                        });

                    }
                });
            } catch (FileNotFoundException e) {
                toast("上架失败");
                al.dismiss();
                e.printStackTrace();
            }
        }else {
            toast("不能为空，检查后重新输入");
            al.dismiss();
            return;
        }
    }
    private void showDialog() {
        LayoutInflater inflater = getLayoutInflater();
        al = new AlertDialog.Builder(getContext())
                .setTitle("iTell")
                .setView(R.layout.dialog)
                .show();
    }
    public void toast(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
